package com.example.timetable.dialog;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;

import com.example.timetable.R;
import com.example.timetable.dao.Course;
import com.example.timetable.databinding.AddCourseItemBinding;

public class AddCourseDialog {

    public static void addCourse(Activity context,AddCourseListener listener) {
        final View inflate = AddCourseItemBinding.inflate(context.getLayoutInflater()).getRoot();
        new AlertDialog.Builder(context)
                .setView(inflate)
                .setPositiveButton("添加", (dialog, which) -> {
                    String courseName = ((EditText) inflate.findViewById(R.id.etCourseName)).getText().toString();
                    String teacherName = ((EditText) inflate.findViewById(R.id.etTeacherName)).getText().toString();
                    String startWeek = ((EditText) inflate.findViewById(R.id.etStartWeek)).getText().toString();
                    String endWeek = ((EditText) inflate.findViewById(R.id.etEndWeek)).getText().toString();
                    if ("".equals(courseName) || "".equals(startWeek) || "".equals(endWeek)) {
                        Toast.makeText(context, "课程名和周次信息不可为空！", Toast.LENGTH_SHORT).show();
                    } else {
                        Course course = new Course();
                        course.setCourseName(courseName);
                        course.setTeacherName(teacherName);
                        course.setStartWeek(Integer.parseInt(startWeek));
                        course.setEndWeek(Integer.parseInt(endWeek));
                        boolean save = course.save();
                        listener.state(save);
                    }
                })
                .setNegativeButton("取消", null)
                .create()
                .show();
    }

    public interface AddCourseListener{
        /**
         * 添加课程成功状态
         */
        void state(Boolean save);
    }

}
